// pages/all/all.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    navbar: ['已加群', '未加群'],
    currentTab: 0,
    items: [],
    startX: 0, //开始坐标
    startY: 0,
    goinGroup: '',
    copyCode:'1111',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    wx.setNavigationBarTitle({
      title: getApp().globalData.title
    })
    
    wx.request({
      url: getApp().globalData.baseURL + '/api/search',
      method: 'GET',
      data: {
        q: getApp().globalData.conditions.q,
        shortcut: getApp().globalData.conditions.shortcut,
        page: getApp().globalData.conditions.page,
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-TOKEN': getApp().globalData.token
      },
      success: function (res) {
        if (res.statusCode == 403) {
          wx.redirectTo({
            url: '../index/index',
          });
          return;
        }
        if (res.statusCode != 200 || !res.data.success) {
          wx.showToast({
            title: '请稍后重试',
            icon: 'none',
            duration: 1000
          });
          return;
        }
        
        _this.setData({
          items: res.data.items,
          page: res.data.request.page
        })
      }
    });
    
  },


  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.items.forEach(function (v, i) {
      if (v.isTouchMove)//只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      items: this.data.items
    })
  },
  goSearch: function(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  //滑动事件处理
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index,//当前索引
      startX = that.data.startX,//开始X坐标
      startY = that.data.startY,//开始Y坐标
      touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
      //获取滑动角度
      angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
    that.data.items.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      items: that.data.items
    })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  // 一键复制事件
  copyBtn: function (e) {
    var that = this;
    wx.setClipboardData({
      //准备复制的数据
      data: e.currentTarget.dataset.wxcode,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  },

  //改变添加状态
  changeType: function (e) {
    var orderId = e.currentTarget.dataset.no;
    var tag = e.currentTarget.dataset.tag;
    var _this = this;
    wx.request({
      url: getApp().globalData.baseURL + '/api/wechat/tag',
      method: 'GET',
      data: {
        orderId: orderId,
        tag: tag
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-TOKEN': getApp().globalData.token
      },
      success: function (res) {
        if (res.statusCode == 403) {
          wx.redirectTo({
            url: '../index/index',
          });
          return;
        }
        if (res.statusCode != 200 || !res.data.success) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1000
          });
          return;
        }
        wx.showToast({
          title: '标记成功',
          icon: 'none',
          duration: 1000
        });
        var data = _this.data.items.map(function(value, index) {
            if (value.orderId == orderId) value.customerWechat = tag;
            return value;
        })
        //更新数据
        _this.setData({
          items: data
        })
        getApp().globalData.conditions.page = 0;
        _this.onLoad();
        return;
      }
      
    });

    
  },
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  // onScrollLower: function(e) {
    
  // },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '重新加载...',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000);
    //init global conditions
    getApp().globalData.conditions.page = 0;
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: getApp().globalData.baseURL + '/api/search',
      data: {
        page: getApp().globalData.conditions.page + 1,
        shortcut: getApp().globalData.conditions.shortcut,
        q: getApp().globalData.conditions.q,
      },
      method: 'GET',
      // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json',
        'X-TOKEN': getApp().globalData.token
      }, // 设置请求的 header
      success: function (res) {
        wx.hideLoading();
        // success
        if (res.statusCode == 403) {
          wx.redirectTo({
            url: '../index/index',
          });
          return;
        }
        if (res.statusCode != 200 || !res.data.success) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1000
          });
          return;
        }

        if (res.data.items.length == 0) {
          wx.showToast({
            title: '已经拉到底啦...',
          });
          return;
        }
        _this.setData({
          items: _this.data.items.concat(res.data.items)
        })
        getApp().globalData.conditions.page++;
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
        console.log('complete...');
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }) 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})